knitr::opts_chunk$set(echo = TRUE)

# Load libraries for homework problems
library(gapminder) # for data
library(ggplot2)   # for graphs
library(readr)     # for reading in solutions
library(gganimate) # for animation
library(gifski)    # for animation
library(ggrepel)    # for animation

Problem 1

We will be using a curated version of the gapminder dataset.

  1. Create a logical vector using gapminder that is TRUE if gapminder$year is equal to 2007 and FALSE otherwise.

  2. Use the vector to create a subset of the gapminder data, containing only the rows from the year 2007. Call this subset of data gapminder_2007

Note: If you get stuck on this problem, you can proceed to the next problems using the gapminder_2007 object produced by the solution code below.

Your 2007 data should look like this:

gapminder_2007 <- gapminder[which(gapminder$year==2007),]

gapminder_2007

Problem 2

Create a plot using the ggplot2 package:

  • use the ggplot() function and specify data = gapminder_2007.

  • use the aes() function:

    • y axis should be lifeExp (life expectancy)

    • x axis should be gdpPercap (gross domestic product per capita; a measure of income)

    • the fill of objects plotted should correspond with continent values.

    • the size of objects plotted should correspond with pop values.

  • use geom_point(shape = 21) to get points with visible fill colors.

  • adjust the x-axis to be on the log 10 scale.

The finished figure will look like this:

soln<-ggplot(gapminder_2007, aes(x=gdpPercap,y=lifeExp,fill=continent, size=pop))+geom_point(shape=21)+
  scale_x_log10() 
soln

Problem 3

Make some aesthetic changes to the figure.

  • add labels to the fill, scale, title, subtitle, and axes of the figure.

  • use a theme_() function of your choice. (mine is theme_minimal())

  • use the theme() function to increase the size of all text in the plot.

The finished figure will look like this:

soln<-soln +
  labs(title="Gapminder data from 2007",
       subtitle = "Country-specific points are proportional to population size",
       x="Income (GDP per captia, log10 scale)",
       y="Life expectancy at birth, years",
       fill="Continent",
       size="Population (millions)") +theme_minimal()
soln

Problem 4

Make a couple more adjustments to the figure:

  • Remove the point size guideline from the legend.

  • Increase the size of points in the fill portion of the legend. (hint: look up guides)

soln<-soln+
       guides(size="none", fill=guide_legend(override.aes = list(size=10))) 
soln

Problem 5

  • Re-create the figure from problem 4 using the entire gapminder dataset instead of the gapminder_2007 data.

  • Use the gganimate package to make a smooth animation that has frames based on the year column.

Notes:

  • You can find a great gganimate tutorial here

  • For your own exploration and fun, you can install the ggrepel package and try to attach labels to individual countries.

soln<-ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,fill=continent, size=pop,label=country))+
  geom_point(shape=21)+
  scale_x_log10() +
  labs(title="Gapminder data from {frame_time}",
       subtitle = "Country-specific points are proportional to population size",
       x="Income (GDP per captia, log10 scale)",
       y="Life expectancy at birth, years",
       fill="Continent",
       size="Population (millions)") +
  theme_minimal()+
      guides(size="none", fill=guide_legend(override.aes = list(size=10)))+
      transition_time(year)+
  geom_text_repel()

animate(soln,
  nframes = 250,
  fps = 20,
  width = 96 * 8,
  height = 96 * 6)